package com.magic.smartcity.view.news;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.magic.smartcity.R;
import com.magic.smartcity.beans.NewsCategoryBean;
import com.magic.smartcity.beans.PressBean;
import com.magic.smartcity.beans.TocationBean;
import com.magic.smartcity.interfaces.INewsCallBack;
import com.magic.smartcity.interfaces.INewsPresenter;
import com.magic.smartcity.view.adapters.HomeGuideImageAdapter;
import com.magic.smartcity.view.adapters.HomeNewsAdapter;
import com.magic.smartcity.view.adapters.HomeNewsCategoryAdapter;
import com.youth.banner.Banner;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class NewsFragment extends Fragment implements INewsCallBack {

    @BindView(R.id.news_guide_image)
    Banner mHome_guide_image;
    @BindView(R.id.news_news_category_ry)
    RecyclerView news_category;
    @BindView(R.id.news_news_topic)
    RecyclerView mRecyclerView_news_topic;

    private HomeNewsAdapter mNewsAdapter;
    private HomeGuideImageAdapter mHomeGuideImageAdapter;
    private HomeNewsCategoryAdapter mHomeNewsCategoryAdapter;

    @Inject
    INewsPresenter mPresenter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news, container, false);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {

        //轮播图
        mHomeGuideImageAdapter = new HomeGuideImageAdapter(null);
        mHome_guide_image.setAdapter(mHomeGuideImageAdapter);
        //新闻专栏
        //新闻分类
        mHomeNewsCategoryAdapter = new HomeNewsCategoryAdapter();
        news_category.setAdapter(mHomeNewsCategoryAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        news_category.setLayoutManager(linearLayoutManager);
        mHomeNewsCategoryAdapter.setOnClickItem(position -> {
            mPresenter.getNewList(position);
        });
        //新闻列表
        mNewsAdapter = new HomeNewsAdapter();
        mRecyclerView_news_topic.setAdapter(mNewsAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView_news_topic.setLayoutManager(layoutManager);
        mRecyclerView_news_topic.setHasFixedSize(true);

        mPresenter.registerCallBack(this);
        mPresenter.getNewList(-1);//获取新闻数据
        mPresenter.getRotationImages();//获取轮播图数据
        mPresenter.getNewCategory();//获取新闻分类数据

    }

    ///////////////////////////回调接口实现


    @Override
    public void NewsLoaded(List<PressBean.RowsBean> pressList) {
        mNewsAdapter.setData(pressList);
    }

    @Override
    public void NewsTocation(List<TocationBean.RowsBean> tocationList) {
        mHomeGuideImageAdapter.setDatas(tocationList);
    }

    @Override
    public void NewsCategoryLoaded(List<NewsCategoryBean.DataBean> dataBeanList) {
        mHomeNewsCategoryAdapter.setData(dataBeanList);
    }

    @Override
    public void error(String str) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.unRegisterCallBack(this);
    }
}